class Solution {
    public int dfs(int i, int j, int[][] grid, int n, int m) {
        if (i >= n || i < 0 || j >= m || j < 0 || grid[i][j] == 0)
            return 0;
        int v = grid[i][j];
        grid[i][j] = 0;
        return v + dfs(i + 1, j, grid, n, m) + dfs(i - 1, j, grid, n, m) + dfs(i, j + 1, grid, n, m)
                + dfs(i, j - 1, grid, n, m);
    }

    public int findMaxFish(int[][] grid) {
        int ans = 0;
        int n = grid.length;
        int m = grid[0].length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int tmp = dfs(i, j, grid, n, m);
                ans = ans > tmp ? ans : tmp;
            }
        }
        return ans;
    }
}